﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using CardProduce.Card;
using PersonalHealth.Data.IService;
using PersonalHealth.Data.Service;
using PersonalHealth.Data.Model;
using UI.Comm.Controls;

namespace HealthManagement.Views
{
    /// <summary>
    /// UserCardBind.xaml 的交互逻辑
    /// </summary>
    public partial class UserCardBind : PMWindow
    {
        BackgroundWorker worker1;
        UInt32 kh = 0;
        string password = "FFFFFFFFFFFF";
        MeasureUser measureUser;
        public UserCardBind(MeasureUser measureUser)
        {
            InitializeComponent();
            this.measureUser=measureUser;
            this.Loaded+=new RoutedEventHandler(UserCardBind_Loaded);
            this.Unloaded += new RoutedEventHandler(UserCardBind_Unloaded);
        }

        void UserCardBind_Unloaded(object sender, RoutedEventArgs e)
        {
            if (worker1 != null)
            {
                worker1.CancelAsync();
            }
        }

        void UserCardBind_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                CardSimpleAPI.ConnectByPassword(password, password);
            }
            catch (Exception)
            {
                return;
            }
            worker1 = new BackgroundWorker();
            worker1.WorkerSupportsCancellation = true;
            worker1.DoWork += (o1, e1) =>
            {
                while (true)
                {
                    while (CardSimpleAPI.RequestIDLE() != 0)
                    {
                        if (worker1.CancellationPending)
                        {
                            e1.Cancel = true;
                            return;
                        }
                        System.Threading.Thread.Sleep(500);
                    }
                    byte size = 0;
                    byte status = CardAPI.CascAnticoll(0, CardAPI.AntiCollision1, ref kh);
                    if (status != 0)
                        throw new CardException(CardAPI.GetErrorMsg(status));
                    status = CardAPI.CascSelect(CardAPI.AntiCollision1, kh, ref size);
                    if (status != 0)
                        throw new CardException(CardAPI.GetErrorMsg(status));
                    if (kh > 0)
                    {
                        break;
                    }
                }
            };
            worker1.RunWorkerCompleted += (o2, e2) =>
            {
                if (!e2.Cancelled)
                {
                    this.Dispatcher.Invoke(new Action(() =>
                    {
                        IUserService service = new UserService();
                        var obj = service.Find(c => c.IcCard == kh.ToString()).Count;
                        if (obj > 0)
                        {
                            MessageBox.Show("此卡号已经使用！");
                            this.Close();
                            return;
                        }
                        measureUser.IcCard = kh.ToString();
                        service.Update(measureUser);
                        this.Close();
                    }), null);
                }
            };
            worker1.RunWorkerAsync();
        }
    }
}
